//
// Created by 11510 on 2025/10/2.
//

#include "LsCommand.h"
#include "RegistryContainer.hpp"
#include "CameraApp.h"

xclib_client::LsCommand::LsCommand(CommandContext ctx) : ICommand("ls", ctx) {}

std::optional<std::future<int>> xclib_client::LsCommand::run(const std::vector<std::string> &args, bool wait) {
    cxxopts::Options options("ls", "ls command");
    options.add_options()
            ("u,unit", "unit", cxxopts::value<uint16_t>()->default_value(""))
            ("help", "print help");
    try {
        auto result = parseOptions(options, args);
        if (result.count("help")) {
            std::cout << options.help() << '\n';
            return std::nullopt;
        }
        auto u = result["unit"].as<uint16_t>();
        auto task = [u]() {
            std::cout << "Listing for unit='" << u << "'...\n";
            registry::RegistryContainer::instance().get<CameraApp>("lazy")->ls(u);
            return 0;
        };
        if (!wait) return make_optional(std::async(std::launch::async, task));
        else {
            task();
            return std::nullopt;
        }
    } catch (const cxxopts::exceptions::exception &e) {
        std::cerr << "ls: option parse error: " << e.what() << '\n';
        return std::nullopt;
    }
}

std::string xclib_client::LsCommand::shortHelp() const { return "ls -u <unitmap> -a <attribute>"; }

std::string xclib_client::LsCommand::longHelp() const { return "ls -u <unitmap> -a <attribute>\n    list registers or attributes"; }